package buzzword.gui;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JList;

import json.JSONException;
import buzzword.GameEndedException;
import buzzword.NoGameHostException;
import buzzword.ServerCommunicationException;
import javax.swing.JTextField;

public class CreateGamePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JScrollPane scrollPaneRows = null;
	private JScrollPane scrollPaneColumns = null;
	private JLabel labelRows = null;
	private JLabel labelColumns = null;
	private JLabel labelCategory = null;
	private JButton buttonStartGame = null;
	private JButton buttonCreateGame = null;
	private JButton buttonBack = null;
	private JScrollPane scrollPaneCategory = null;
	private JList listRows = null;
	private JList listColumns = null;
	private JList listCategorys = null;
	private JTextField textFieldGameName = null;
	/**
	 * This is the default constructor
	 */
	public CreateGamePanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		labelCategory = new JLabel();
		labelCategory.setBounds(new Rectangle(195, 15, 121, 31));
		labelCategory.setText("Category");
		labelColumns = new JLabel();
		labelColumns.setBounds(new Rectangle(105, 15, 61, 31));
		labelColumns.setText("Columns");
		labelRows = new JLabel("Rows", SwingConstants.CENTER);
		labelRows.setBounds(new Rectangle(15, 15, 61, 31));
		this.setSize(500, 300);
		this.setLayout(null);
		this.setOpaque(false);
		this.add(getScrollPaneRows(), null);
		this.add(getScrollPaneColumns(), null);
		this.add(labelRows, null);
		this.add(labelColumns, null);
		this.add(labelCategory, null);
		this.add(getButtonStartGame(), null);
		this.add(getButtonCreateGame(), null);
		this.add(getButtonBack(), null);
		this.add(getScrollPaneCategory(), null);
		this.add(getTextFieldGameName(), null);
	}

	/**
	 * This method initializes scrollPaneRows	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrollPaneRows() {
		if (scrollPaneRows == null) {
			scrollPaneRows = new JScrollPane();
			scrollPaneRows.setBounds(new Rectangle(15, 60, 61, 211));
			scrollPaneRows.setViewportView(getListRows());
		}
		return scrollPaneRows;
	}

	/**
	 * This method initializes scrollPaneColumns	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrollPaneColumns() {
		if (scrollPaneColumns == null) {
			scrollPaneColumns = new JScrollPane();
			scrollPaneColumns.setBounds(new Rectangle(105, 60, 61, 211));
			scrollPaneColumns.setViewportView(getListColumns());
		}
		return scrollPaneColumns;
	}

	/**
	 * This method initializes buttonStartGame	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonStartGame() {
		if (buttonStartGame == null) {
			buttonStartGame = new JButton();
			buttonStartGame.setBounds(new Rectangle(345, 60, 136, 46));
			buttonStartGame.setText("Start Game");
			buttonStartGame.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						GuiMainFrame.getClient().startGame();
						buttonStartGame.setEnabled(false);
						((GuiMainFrame)SwingUtilities.getRoot(CreateGamePanel.this)).switchActionPanel(new GameBoardPanel());
						//TODO Spielbrett laden
					} catch (NoGameHostException e1) {

						e1.printStackTrace();
					} catch (JSONException e1) {

						e1.printStackTrace();
					} catch (ServerCommunicationException e1) {

						e1.printStackTrace();
					} catch (GameEndedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return buttonStartGame;
	}

	/**
	 * This method initializes buttonCreateGame	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonCreateGame() {
		if (buttonCreateGame == null) {
			buttonCreateGame = new JButton();
			buttonCreateGame.setBounds(new Rectangle(345, 120, 136, 46));
			buttonCreateGame.setText("Create Game");
			buttonCreateGame.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					buttonCreateGame.setEnabled(false);
					try {
						GuiMainFrame.getClient().createGame((Integer)listColumns.getSelectedValue(), (Integer) listRows.getSelectedValue(), (String) listCategorys.getSelectedValue(), textFieldGameName.getText());
					} catch (JSONException e1) {

						e1.printStackTrace();
					}
				}
			});
		}
		return buttonCreateGame;
	}

	/**
	 * This method initializes buttonBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonBack() {
		if (buttonBack == null) {
			buttonBack = new JButton();
			buttonBack.setBounds(new Rectangle(345, 225, 136, 46));
			buttonBack.setText("Back");
			buttonBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					((GuiMainFrame)SwingUtilities.getRoot(CreateGamePanel.this)).switchActionPanel(new MainMenuPanel());
				}
			});
		}
		return buttonBack;
	}

	/**
	 * This method initializes scrollPaneCategory	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrollPaneCategory() {
		if (scrollPaneCategory == null) {
			scrollPaneCategory = new JScrollPane();
			scrollPaneCategory.setBounds(new Rectangle(195, 60, 121, 211));
			scrollPaneCategory.setViewportView(getListCategorys());
		}
		return scrollPaneCategory;
	}

	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getListRows() {
		Integer[] numbrOfRows = {1,2,3,4,5,6};
		if (listRows == null) {
			listRows = new JList(numbrOfRows);
		}
		return listRows;
	}

	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getListColumns() {
		Integer[] numbrOfColumns = {1,2,3,4,5,6};
		if (listColumns == null) {
			listColumns = new JList(numbrOfColumns);
		}
		return listColumns;
	}

	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getListCategorys() {
		String[] categorys = {"animals"};
		if (listCategorys == null) {
			listCategorys = new JList(categorys);
		}
		return listCategorys;
	}

	/**
	 * This method initializes textFieldGameName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTextFieldGameName() {
		if (textFieldGameName == null) {
			textFieldGameName = new JTextField();
			textFieldGameName.setBounds(new Rectangle(347, 179, 133, 32));
			textFieldGameName.setText("newGame");
		}
		return textFieldGameName;
	}

}
